<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

define('__ROOT__',dirname(dirname(__FILE__)));
require_once(__ROOT__.'/objects/measure_info.php');


//////////////////////////////////////////////////
// USER CONTROLLER
//////////////////////////////////////////////////
class Team extends TMT_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	/////////////////////////////////
	// INDEX FUNCTION
	//  
	/////////////////////////////////
	function myteam()
	{
		$this->index();
	}
	
	function index()
	{
		$this->headline = 'Mein Team - Personalübersicht:';
		$this->show_sidebar = FALSE;
	
		$this->load->library('table');
		$this->load->helper('form','url','input');
		
		$teamroles 			= $this->employee->teamrole();
		$active_teamrole 		= $teamroles[$this->active_team];
		$this->view_data['team']	= Model\Team::find($active_teamrole->team_id)->with('employee')->with('stopedemand');
	}
	
	public function activate_stope()
	{
		// Wenn der Activate Trigger von woanders ausgelöst wurde(z.B. von 'show_memeber')
		$target = $this->input->get('target');

		//$this->load->model('team/stope_model');
		$this->load->helper('url');		

		if($this->uri->segment(3) != '')
		{
			$demand = Model\Stopedemand::find($this->uri->segment(3));
			$demand->inactive = 1;
			$demand->save();
			//$this->stope_model->activate_stope($this->uri->segment(3));
		}
		//else do nothing, because the function has been called without parameters
		
		redirect('team/'.$target);
		
		
	}
	
	public function deactivate_stope()
	{
		// Wenn der Activate Trigger von woanders ausgelöst wurde(z.B. von 'show_memeber')
		$target = $this->input->get('target');

		//$this->load->model('team/stope_model');
		$this->load->helper('url');		

		if($this->uri->segment(3) != '')
		{
			$demand = Model\Stopedemand::find($this->uri->segment(3));
			$demand->inactive = 0;
			$demand->save();
			//$this->stope_model->deactivate_stope($this->uri->segment(3));
		}
		//else do nothing, because the function has been called without parameters
		
		redirect('team/'.$target);

	}
	
	
	/**
	 * @todo: Add a functionality to check the rights of the viewer
	 */
	function show_member($id = FALSE)
	{
            if ($id === FALSE) {
                redirect('team');
            }

            $data['sm_status'] = $this->input->get('sm_status');

            
            $this->view_data['all_jobclusters'] = Model\Jobcluster::get_assoc_array();
            $this->view_data['all_expertgroups'] = Model\Expertgroup::get_assoc_array();
            
            $mode = $this->input->get('mode');
            if($mode && $mode === 'edit_istplan')
                $this->view_data['mode'] = $mode;

            $this->view_data['teammember'] = Model\Employee::find($id);
            $this->view_data['target'] = __FUNCTION__ . '/' . $this->view_data['teammember']->id;

            $this->view_data['form']['comment'] = array('name' => 'comment',
                'rows' => '5',
                'cols' => '10',
                'style' => 'width:70%;');
            $this->view_data['form']['add_comment'] = array('type' => 'submit',
                'name' => 'add_comment',
                'value' => 'Speichern',
                'class' => 'btn btn-success btn-small');

            $this->view_data['form']['action'] = __CLASS__ . '/add_comment/' . $id;

            $this->headline = 'Mitarbeiterdetails - ' . $this->view_data['teammember']->contactdata()->surname . ', ' . $this->view_data['teammember']->contactdata()->forename;
	}
        
        function edit_istplan()
        {
            $id     = $this->input->post('teammember_id');
            $act_jc = $this->input->post('act_jc');
            $tar_jc = $this->input->post('tar_jc');
            $act_eg = $this->input->post('act_eg');
            $tar_eg = $this->input->post('tar_eg');
            
            $member = Model\Employee::find($id);
            
            $member->actual_jobcluster_id = $act_jc;
            $member->target_jobcluster_id = $tar_jc;
            $member->actual_expertgroup_id = $act_eg;
            $member->target_expertgroup_id = $tar_eg;
            
            $member->save();
            
            redirect('team/show_member/'.$id);      
            
        }
	/**
	 * 
	 */
	function switch_measure()
	{
		$stope_id = $this->input->post('stopeid');
		$ft_id    = $this->input->post('ft_id');
		$ma_id    = $this->input->post('ma_id');
		
		if(empty($stope_id) || empty($ft_id))
		{
			redirect('');
		}
		
		$this->load->model('team/stope_model');
		
		// If the update process succeded
		if($this->stope_model->replace_specific_stope($stope_id, $ft_id))
		{
			redirect('/team/show_member/'.$ma_id.'?sm_status=success');
		}
	}
        
	public function add_comment($employee_id = FALSE)
	{
		if(!$employee_id)
			redirect('team');
		
		$employee = Model\Employee::find($employee_id,FALSE);
	
		
		if($employee && $text = $this->input->post('comment'))
		{
			$note = Model\Note::make(array('text' => $text, 'employee_id' => $employee_id));
			$note->save();
		}
		
		redirect(__CLASS__.'/show_member/'.$employee_id);

	}
	
}	
/* End of file team.php */
/* Location: ./application/controllers/team.php */